package br.com.projeto.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class HttpClientJson {

	private static final String URL = "http://webservice.claudiomedeiros.net/loterias/megasena.json";

	public static String httpGet() throws IllegalStateException, IOException {
		StringBuilder stringBuilder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(URL);

		HttpResponse response = client.execute(httpGet);
		StatusLine statusLine = response.getStatusLine();
		int statusCode = statusLine.getStatusCode();

		if (statusCode == 200) {
			HttpEntity entity = response.getEntity();
			InputStream content = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(content));

			String line;
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line);
			}
		} else {
			Log.e("Json", "Erro durante o download do arquivo.");
		}
		return stringBuilder.toString();
	}
}
